<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class AdminModel extends Model
{
    protected $table = 'admin';
    //不更新时间
    public $timestamps = false;

    //验证
    public function checkLogin($admin_name , $admin_pass) {

        $user = self::where('admin_name' , $admin_name)->select('admin_pass' , 'salt')->first();
        if ( !$user ) {
            return [
                'msg' => '没有此用户名',
                'status' => 'fail'
            ];
        }

        $user = $user->toArray();
        //用户输入的密码拼接salt盐 在进行MD5加密
        $pass = md5($admin_pass.$user['salt']);

        if ( $user['admin_pass'] == $pass) {
            return [
                'msg' => '登录成功',
                'status' => 'success',
            ];
        }
        return ['msg' => '密码不正确' , 'status' => 'fail'];
    }

    //检测密码是否正确
    public function checkPass($username , $password) {
        $res = $this->checkLogin($username , $password);
        return $res;
    }

    //更新管理员密码
    public function updatePass($username , $new_pass) {
        //随机生成盐
        //str_random不可用
        $salt = \Str::random(6);
        //md5加密
        $admin_pass = md5($new_pass.$salt);
        //更新
        $res = self::where('admin_name' , $username)->update(['salt' => $salt , 'admin_pass' => $admin_pass]);

        return $res;
        
    }
}
